package com.ruoyi.info.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.CmsContent;
import com.ruoyi.info.service.ICmsContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻Controller
 *
 * @author xwj
 * @date 2023-07-07
 */
@Controller
@RequestMapping("/news/content")
public class CmsContentController extends BaseController
{
    private String prefix = "news/content";

    @Autowired
    private ICmsContentService cmsContentService;

    @GetMapping()
    public String content()
    {
        return prefix + "/content";
    }

    /** -------------- 新闻 -------------- */
    @GetMapping("/news")
    public String news() {
        return prefix + "/news";
    }
    @GetMapping("/newsAdd")
    public String newsAdd() {
        return prefix + "/newsAdd";
    }
    @GetMapping("/newsEdit/{id}")
    public String newsEdit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/newsEdit";
    }

    /**
     * 查询新闻列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsContent cmsContent)
    {
        startPage();
        List<CmsContent> list = cmsContentService.selectCmsContentList(cmsContent);
        return getDataTable(list);
    }

    /**
     * 导出新闻列表
     */
    @Log(title = "新闻", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsContent cmsContent)
    {
        List<CmsContent> list = cmsContentService.selectCmsContentList(cmsContent);
        ExcelUtil<CmsContent> util = new ExcelUtil<CmsContent>(CmsContent.class);
        return util.exportExcel(list, "新闻数据");
    }

    /**
     * 新增新闻
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻
     */
    @Log(title = "新闻", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsContent cmsContent)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        cmsContent.setStatus("0");
        cmsContent.setUuid(uuid);

        int i = cmsContentService.insertCmsContent(cmsContent);
        return toAjax(i);
    }

    /**
     * 修改新闻
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/edit";
    }

    /**
     * 预览
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/newsPreview/{id}")
    public String newsPreview(@PathVariable("id") Long id, ModelMap mmap){
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/newsPreview";
    }
    /**
     * 修改保存新闻
     */
    @Log(title = "新闻", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsContent cmsContent)
    {
        return toAjax(cmsContentService.updateCmsContent(cmsContent));
    }

    /**
     * 删除新闻
     */
    @Log(title = "新闻", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsContentService.deleteCmsContentByIds(ids));
    }

    /** -------------- 安全摘要 -------------- */
    @GetMapping("/safe")
    public String safe() {
        return prefix + "/safe";
    }
    @GetMapping("/safeAdd")
    public String safeAdd() {
        return prefix + "/safeAdd";
    }
    @GetMapping("/safeEdit/{id}")
    public String safeEdit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/safeEdit";
    }


    /** -------------- 病毒预报 -------------- */
    @GetMapping("/virus")
    public String virus() {
        return prefix + "/virus";
    }
    @GetMapping("/virusAdd")
    public String virusAdd() {
        return prefix + "/virusAdd";
    }
    @GetMapping("/virusEdit/{id}")
    public String virusEdit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/virusEdit";
    }

    /** -------------- 高峰论坛 -------------- */
    @GetMapping("/forum")
    public String forum() {
        return prefix + "/forum";
    }
    @GetMapping("/forumAdd")
    public String forumAdd() {
        return prefix + "/forumAdd";
    }
    @GetMapping("/forumEdit/{id}")
    public String forumEdit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/forumEdit";
    }


    /** -------------- 关于我们 -------------- */
    @GetMapping("/about")
    public String about() {
        return prefix + "/about";
    }
    @GetMapping("/aboutAdd")
    public String aboutAdd() {
        return prefix + "/aboutAdd";
    }
    @GetMapping("/aboutEdit/{id}")
    public String aboutEdit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsContent cmsContent = cmsContentService.selectCmsContentById(id);
        mmap.put("cmsContent", cmsContent);
        return prefix + "/aboutEdit";
    }
}
